<?php

namespace app\api\controller;

use app\admin\model\plan\User as PlanUser;
use app\common\controller\Api;
use app\common\model\plan\Plan as PlanPlan;


/**
 * 培训计划接口
 */
class Plan extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @date: 2024-03-26 
     * @ApiTitle (培训计划列表)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="int", required=true, sample="", description="1=线上课程,2=线下课程")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function plan_list()
    {
        $params = request()->param();
        extract($params);
        if (isset($keywords) && $keywords) {
            $where['title'] = ['like', '%' . $keywords . '%'];
        }
        $where['type'] = $type;
        $where['acct_id'] = $this->auth->acct_id;
        $lists = PlanPlan::where($where)->paginate($limit ?? 10);
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2024-03-26 
     * @ApiTitle (培训计划详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="分类ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function plan_info()
    {
        $params = request()->param();
        extract($params);
        $info = PlanPlan::get($id);
        $info->is_sign = PlanUser::where('p_id', $id)->where('user_id', $this->auth->id)->count() ? true : false;
        $this->success('', $info);
    }

    /**
     * @Author LK
     * @date: 2024-04-08 
     * @ApiTitle (报名培训计划)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="课程id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function res_plan()
    {
        $params = request()->param();
        extract($params);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $uid = $this->auth->id;
        $course = PlanPlan::get($id);
        if (!$course) {
            $this->error('培训计划不存在');
        }
        if ($course['endtime'] < time()) {
            $this->error('培训计划报名时间已结束');
        }
        if ($course['total_num'] <= PlanUser::where('c_id', $id)->count()) {
            $this->error('报名人数已满');
        }
        $info = PlanUser::get(['p_id' => $id, 'user_id' => $uid, 'acct_id' =>  $this->auth->acct_id]);
        if ($info) {
            $this->error('已报名');
        }
        $res = PlanUser::create([
            'p_id' => $id,
            'user_id' => $uid,
            'acct_id' => $this->auth->acct_id
        ]);
        if ($res) {
            $this->success('报名成功');
        }
        $this->error('报名失败');
    }
    /**
     * @Author LK
     * @date: 2024-04-09 
     * @ApiTitle (取消报名)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */

    public function cancel_sign()
    {
        $params = request()->param();
        extract($params);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $uid = $this->auth->id;
        $info = PlanUser::get(['c_id' => $id, 'user_id' => $uid, 'acct_id' =>  $this->auth->acct_id]);
        if (!$info) {
            $this->error('未报名');
        }
        $res = $info->delete();
        if ($res) {
            $this->success('取消成功');
        }
        $this->error('取消失败');
    }


}
